<script setup lang="ts">
import {onMounted, ref} from "vue";
import {getById, updateById, useGetMaterialTypeListAPI} from "@/apis/materialType.js";
import {ElMessage} from "element-plus";
import router from "@/router/index.js";
import {useRoute} from "vue-router";


const route = useRoute();
const materialTypeId=route.params.id;


const materialTypeList=ref<any>([]);
const uniqueParentTypes = ref<any>([]);
const uniqueChildTypes = ref<any>([]);


const materialTypeParent=ref('');
const materialTypeChild=ref('');

const materialTypeInfo=ref<any>({
  parentType:'',
  childType:'',
  description:'',
})



onMounted(async ()=>{
  const res=await useGetMaterialTypeListAPI(1,10000);
  materialTypeList.value=res.data;
  // 过滤掉相同的 parentType
  const seen = new Set();
  uniqueParentTypes.value = materialTypeList.value.filter(item => {
    if (!seen.has(item.parentType)) {
      seen.add(item.parentType);
      return true;
    }
    return false;
  });

  const resMaterialType=await getById(Number(materialTypeId));
  materialTypeInfo.value=resMaterialType.data;
  materialTypeParent.value=resMaterialType.data.parentType;
  materialTypeChild.value=resMaterialType.data.childType;
})

// 处理父类型选择变化
const handleParentChange = (value: string) => {
  console.log(value);
  if (!uniqueParentTypes.value.some(item => item.parentType === value)) {
    const newId = Math.max(...uniqueParentTypes.value.map(item => item.id), 0) + 1;
    const newItem = {
      id: newId,
      parentType: value,
      childType: "",
      fullName: "",
      description: "",
    };
    uniqueParentTypes.value.push(newItem);
    materialTypeList.value.push(newItem);
  }
  materialTypeInfo.value.parentType = value;
};

// 处理子类型选择变化
const handleChildChange = (value: string) => {
  if (!uniqueChildTypes.value.some(item => item.childType === value)) {
    const newId = Math.max(...uniqueChildTypes.value.map(item => item.id), 0) + 1;
    const newItem = {
      id: newId,
      parentType: materialTypeInfo.value.parentType,
      childType: value,
      fullName: "",
      description: "",
    };
    uniqueChildTypes.value.push(newItem);
    materialTypeList.value.push(newItem);
  }
  materialTypeInfo.value.childType = value;
};



const updateSummit=async ()=>{
  materialTypeInfo.value.parentType=materialTypeParent.value;
  materialTypeInfo.value.childType=materialTypeChild.value;
  if(materialTypeInfo.value.parentType===''||materialTypeInfo.value.childType===''){
    ElMessage({
      type:'error',
      message:'请选择完整的类型',
    })
    return;
  }

  await updateById(materialTypeInfo.value).then((res: any) => {
    if(res.code===200){
      ElMessage({
        type:'success',
        message:'修改成功',
      })
      router.push("/manage/materialTypeManager");
    }
    else {
      ElMessage({
        type:'error',
        message:res.msg,
      })
    }
  })

}

</script>

<template>
  <!-- 面包屑 -->
  <div class="bread-container">
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: `/` }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{path:`/manage/materialTypeManager`}" >materialType列表</el-breadcrumb-item>
      <el-breadcrumb-item  >修改materialType</el-breadcrumb-item>
    </el-breadcrumb>
  </div>

  <div class="divTable">
    <table class="table">
      <tbody>
      <tr>
        <td class="tableTitle" colspan="4" >修改材料类型</td>
      </tr>
      <tr>
        <td style="width: 250px;height: 15px;text-align: center;background: #EFF6FF;color: #242424;font-family: 微软雅黑,serif">父类型</td>
        <el-select
            @change="handleParentChange"
            clearable
            filterable
            allow-create
            default-first-option
            :reserve-keyword="false"
            v-model="materialTypeParent"
            placeholder="请添加或者选择"
            size="large"
            style="width: 250px;height: 15px;   text-align: center"
        >
          <el-option
              v-for="item in uniqueParentTypes"
              :key="item.id"
              :label="item.parentType"
              :value="item.parentType"
          />
        </el-select>

        <td style="width: 250px;height: 15px; text-align: center ;background: #EFF6FF"  >子类型</td>
        <td style="width: 350px;height: 15px ; text-align: center">
          <el-select
              clearable
              @change="handleChildChange"
              filterable
              allow-create
              default-first-option
              :reserve-keyword="false"
              v-model="materialTypeChild"
              placeholder="请添加或者选择"
              size="large"
          >
            <el-option
                v-for="item in materialTypeList"
                :key="item.id"
                :label="item.childType"
                :value="item.childType"
            />
          </el-select></td>
      </tr>

      <tr>

        <!--        </td>-->
        <td style="width: 350px;height: 15px;text-align: center ;background: #EFF6FF"  >备注</td>
        <td colspan="3" style="width: 350px;height: 15px;text-align: center   "><el-input clearable v-model="materialTypeInfo.description" /></td>
      </tr>
      <tr>
        <td colspan="4" style="background:#EFF6FF ">
          <el-button @click="updateSummit" type="primary"  style="margin-top: 1%;margin-bottom: 1%" >确认修改</el-button>
        </td>
      </tr>
      </tbody>
    </table>
  </div>

</template>

<style scoped lang="scss">

.bread-container {
  padding: 25px 10px;
  margin-left: 10px;
}


.divTable {
  position: static;
  top: 150px;
  bottom: 0;
  left: 300px;
  right: 0;
  margin: auto;
}


table, td,tr{
  border: 1px solid #44a8da;
  text-align: center;
  border-spacing: 0;
  border-collapse: collapse;
  height: 45px;
}

.tableTitle{

  border-right: 0 none;
  border-left: 0 none;
  text-align: center;
  font-size: large;
  font-weight: bold;
}

.demo-image__error .image-slot {
  font-size: 30px;
}
.demo-image__error .image-slot .el-icon {
  font-size: 30px;
}
.demo-image__error .el-image {
  width: 100%;
  height: 200px;
}

.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>